package com.ihteima.utils;


import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ThreadLocal 工具类
 */
@SuppressWarnings("all")
public class ThreadLocalUtil {
    //提供ThreadLocal对象,
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = TransmittableThreadLocal.withInitial(HashMap::new);

    //根据键获取值
    public static <T> T get(){
        Object value = THREAD_LOCAL.get().get("userId");
        if (Objects.isNull(value)) {
            return null;
        }
        return (T) value;
    }
	
    //存储键值对
    public static void set(Object userId){
        THREAD_LOCAL.get().put("userId", userId);
    }


    //清除ThreadLocal 防止内存泄漏
    public static void remove(){
        THREAD_LOCAL.remove();
    }
}
